import { OneModal } from '@/components/one-ui';
import { SchemaManage } from '@/components/SchemaComponent';
import { IOneModalRef } from '@/pages/type';
import { Ref, useEffect, useMemo, useRef, useState } from 'react';
import { getTableColumns } from '../mconfig';
import { procdefDataGridServices } from '../services';
import { WorkflowSwitchAPI } from '../common/type';
import { message } from 'antd';
import '../common/styles.less';
import { TipTextModal } from '@/components/TipsErrorModal';

export const WorkflowNoDefaultTable: React.FC<
  WorkflowSwitchAPI.IWorkflowNoDefaultTable
> = ({
  openVisibleSwitch, //是否打开
  onCloseSwitch,
  orderType, //业务类别
  category, //  总 流程类别
  businessCategory, // 相关业务 的流程类别
  requestController, //流程业务接口 Controller
  saveSelectFlowID, //保存当前选中 的非默认流程 id
  selectFlowID, //选中的非默认流程ID
}) => {
  const workflowSwitchTableRef: any = useRef();

  const prefixCls = 'workflow-switchtable-';

  /**
   * modal ref
   */
  const workflowSwitchModalRef: Ref<IOneModalRef> = useRef(null);

  /**
   * 管理页必有参数
   */
  const mustManageParams: Record<string, any> = {
    orderType, //工单类型
    category, //业务流程类别
    businessCategory, // 总 流程类别
    suspendState: '1', // 是否激活 => 激活
    isDefault: '0', //是否默认 => 否
  };

  /**
   * 关闭弹窗
   */
  const onCloseModal = () => {
    if (workflowSwitchModalRef) {
      if (workflowSwitchModalRef.current) {
        if (workflowSwitchModalRef.current.onModalCancel) {
          workflowSwitchModalRef.current.onModalCancel();
        }
      }
    }
  };

  /**
   * 是否打开提示框
   */
  const [isShowTipsModal, setIsShowTipsModal] = useState<boolean>(false);

  /**
   * 流程列表接口回调
   * @param res
   */
  const procdefDataCallback = (res: any) => {
    if (!(res && res?.msgFlag)) {
      message.error(res?.msg);
    }
  };

  /**
   * 获取流程列表
   */
  const { getProcdefData, pageLoading, pageData, pagination, searchList } =
    procdefDataGridServices(procdefDataCallback, requestController);

  /**
   * 重置搜索表单方法
   */
  const onReset = (values: any) => {
    getProcdefData({ ...values, ...mustManageParams });
  };

  /**
   * 搜索列表
   */
  const onSearch = (request: WorkflowSwitchAPI.IGridRequestType) => {
    getProcdefData({ ...request, ...mustManageParams });
  };

  /**
   * 分页change 方法
   */
  const onPageChange = (current: any, pageSize: any, params: any) => {
    getProcdefData({ pageSize, current, ...params, ...mustManageParams });
  };

  /**
   * 获取columns方法
   */
  const columns: Record<string, any>[] = useMemo(() => {
    return getTableColumns();
  }, []);

  /**
   * 初始化获取数据
   */
  useEffect(() => {
    if (openVisibleSwitch) {
      getProcdefData({ ...mustManageParams });
    }
  }, [openVisibleSwitch]);

  /**
   * 保存选中的表格id
   * @param selecdInfo
   */
  const checkedCallback = (selecdInfo: any, selected: any) => {
    saveSelectFlowID(selected ? selecdInfo?.id : '');
  };

  return (
    <>
      <OneModal
        open={true}
        // getContainer={false}
        ref={workflowSwitchModalRef}
        title={'挑选流程'}
        width={document.body.clientWidth * 0.6}
        bodyStyle={{
          padding: '0px',
          height: document.body.clientHeight * 0.4,
        }}
        centered
        onCancel={() => {
          onCloseSwitch(false);
        }}
        onOk={() => {
          if (selectFlowID) {
            onCloseModal();
            onCloseSwitch(true);
          } else {
            setIsShowTipsModal(true);
          }
        }}
      >
        <div className={prefixCls + 'commonManageCls'}>
          <SchemaManage
            ref={workflowSwitchTableRef}
            searchProps={{
              switchList: [], //头部切换列表
              searchList, //搜索表单列表
              onReset, //重置
              onSwitch: () => {}, //头部切换
              onSearch, //搜索方法
            }}
            actionList={[]} //Table头部操作按钮列表
            tableProps={{
              // table 表格属性
              columns,
              dataSource: pageData?.rows,
              loading: pageLoading,
              pagination: pagination as any,
              rowKey: 'id',
              noShowColumns: true, //取反, true  为不显示
              onPageChange: onPageChange, //分页change方法
              rowSelection: {
                type: 'radio',
                onSelect: checkedCallback,
                selectedRowKeys: selectFlowID ? [selectFlowID] : [],
              } as any,
              onRow: (record: any) => {
                return {
                  onClick: () => {
                    saveSelectFlowID(selectFlowID ? '' : record?.id);
                  },
                };
              },
            }}
          />
        </div>
      </OneModal>

      {/* 提示框 */}
      {isShowTipsModal && (
        <TipTextModal
          onClose={() => {
            setIsShowTipsModal(false);
          }}
          configTestRes={'请选择流程！！'}
        />
      )}
    </>
  );
};
